package com.cn.lotary.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.*;

/**
 *
 *
 * @author Jason 14-Jul-2011
 */
public class EntityProvider2 {

    private static final String IMPORT_LIST = "import java.util.ArrayList;\r\nimport java.util.List;\r\n";

    public static String getType(String type) {
        String outcome = null;
        if ("INT".equals(type)) {
            outcome = "int";
        } else if ("VARCHAR".equals(type)) {
            outcome = "String";
        } else if ("DOUBLE".equals(type) || "BIT".equals(type)) {
            outcome = "Double";
        } else if ("TIMESTAMP".equals(type) || "DATETIME".equals(type)) {
            outcome = "Date";
        } else {
            System.err.println("other type: " + type);
        }
        return outcome;
    }
    public static final String RN = "\r\n";

    public static void main(String[] args) throws IOException {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/crm";
        String user = "root";
        String password = "JasonChen";
        String packagePath = "com.cn.entity";
        String singleTable = null;
        getEntitys(driver, url, user, password, singleTable);

    }

    private static void getEntitys(String driver, String url, String user, String password, String singleTable) {
        Connection con = null;

        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);
            DatabaseMetaData databaseMetaData = con.getMetaData();
            ResultSet tables = databaseMetaData.getTables(null, "%", singleTable != null ? singleTable : "%", new String[]{"TABLE"});
            ResultSet exportedKeys = databaseMetaData.getExportedKeys(null, null, "unit");

            while (exportedKeys.next()) {
                System.out.println(exportedKeys.getString("PKTABLE_CAT"));
                System.out.println(exportedKeys.getString("PKTABLE_SCHEM"));
                System.out.println(exportedKeys.getString("PKTABLE_NAME"));
                System.out.println(exportedKeys.getString("PKCOLUMN_NAME"));
                System.out.println(exportedKeys.getString("FKTABLE_SCHEM"));
                System.out.println(exportedKeys.getString("FKTABLE_NAME"));
                System.err.println(exportedKeys.getString("FKCOLUMN_NAME"));
                System.err.println("******");
            }
            if (false) {
                while (tables.next()) {
                    String tableName = tables.getString("TABLE_NAME");
                    String entityName = ObjectUtil.tableNameToEntityName(tableName);

                    ResultSet importedKeys = databaseMetaData.getImportedKeys(null, null, tableName);
                    Map<String, String> importedMap = new HashMap<String, String>();
                    while (importedKeys.next()) {
                        importedKeys.getString("PKTABLE_CAT");
                        importedKeys.getString("PKTABLE_SCHEM");
                        importedKeys.getString("PKTABLE_NAME");
                        importedKeys.getString("PKCOLUMN_NAME");
                        importedKeys.getString("FKTABLE_CAT");
                        importedKeys.getString("FKTABLE_SCHEM");
                        importedKeys.getString("FKTABLE_NAME");
                        importedKeys.getString("FKCOLUMN_NAME");
                        importedKeys.getString("KEY_SEQ");
                        importedKeys.getString("UPDATE_RULE");
                        importedKeys.getString("DELETE_RULE");
                        importedKeys.getString("FK_NAME");
                        importedKeys.getString("PK_NAME");
                        importedKeys.getString("DEFERRABILITY");
                        importedMap.put(importedKeys.getString("FKCOLUMN_NAME"),
                                importedKeys.getString("PKTABLE_NAME"));
                    }

                    String tableComment = tables.getString("REMARKS");
                    String TABLE_CAT = tables.getString("TABLE_CAT");
                    String TABLE_SCHEM = tables.getString("TABLE_SCHEM");
                    String TABLE_TYPE = tables.getString("TABLE_TYPE");
                    String REMARKS = tables.getString("REMARKS");
                    //  String TYPE_CAT = tables.getString("TYPE_CAT");
                    // String TYPE_SCHEM = tables.getString("TYPE_SCHEM");
                    //  String TYPE_NAME = tables.getString("TYPE_NAME");
                    // String SELF_REFERENCING_COL_NAME = tables.getString("SELF_REFERENCING_COL_NAME");
                    // String REF_GENERATION = tables.getString("REF_GENERATION");




                    ResultSet columns = databaseMetaData.getColumns(null, "%", tableName, "%");

                    while (columns.next()) {
                        String columnName = columns.getString("COLUMN_NAME");
                        String NULLABLE = columns.getString("NULLABLE");
                        String columnComment = columns.getString("REMARKS");
                        String columnType = columns.getString("DATA_TYPE");
                        String columnTYPE_NAME = columns.getString("TYPE_NAME");
                        String COLUMN_SIZE = columns.getString("COLUMN_SIZE");
                        String DECIMAL_DIGITS = columns.getString("DECIMAL_DIGITS");
                        String NUM_PREC_RADIX = columns.getString("NUM_PREC_RADIX");
                        String COLUMN_DEF = columns.getString("COLUMN_DEF");
                        String SQL_DATA_TYPE = columns.getString("SQL_DATA_TYPE");

                        String SQL_DATETIME_SUB = columns.getString("SQL_DATETIME_SUB");
                        String CHAR_OCTET_LENGTH = columns.getString("CHAR_OCTET_LENGTH");
                        String ORDINAL_POSITION = columns.getString("ORDINAL_POSITION");
                        String IS_NULLABLE = columns.getString("IS_NULLABLE");
                        //String SCOPE_CATLOG = columns.getString("SCOPE_CATLOG");
                        String SCOPE_SCHEMA = columns.getString("SCOPE_SCHEMA");
                        String SCOPE_TABLE = columns.getString("SCOPE_TABLE");
                        String SOURCE_DATA_TYPE = columns.getString("SOURCE_DATA_TYPE");
                        String IS_AUTOINCREMENT = columns.getString("IS_AUTOINCREMENT");

                        String fildeName = ObjectUtil.columnNameToFieldName(columnName);
                        String fildeType = getType(columnTYPE_NAME);

                        if (importedMap.keySet().contains(columnName)) {
                            fildeType = ObjectUtil.tableNameToEntityName(importedMap.get(columnName));
                        }

                        ResultSet primaryKeys = databaseMetaData.getPrimaryKeys(null, null, tableName);

                        while (primaryKeys.next()) {
                            primaryKeys.getString("TABLE_CAT");
                            primaryKeys.getString("TABLE_SCHEM");
                            primaryKeys.getString("TABLE_NAME");
                            primaryKeys.getString("KEY_SEQ");
                            primaryKeys.getString("PK_NAME");
                            String key = primaryKeys.getString("COLUMN_NAME");
                        }
                    }

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
}
